/*
    Created by IntelliJ IDEA.
    @Auther: gaozhe
    @Date: 2020/5/10 下午 5:19
    
*/
package gaozhe.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class JsoupSpider2 {
    public static ArrayList getImgArray(String url){
        ArrayList<String> imgsArray = new ArrayList<String>();
        url = new Scanner(System.in).next();
        Document document = null;

        try{
            document = Jsoup.connect(url)
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0")
                    .header("Connection", "keep-alive")
                    .timeout(80000)
                    .get();
        } catch (Exception e) {
            System.out.println("超时");
            e.printStackTrace();
        }


        Elements elementsByClass = document.getElementsByClass("post-bd");
        for (Element byClass : elementsByClass) {
            Elements ignore_js_ops = byClass.getElementsByTag("ignore_js_op");
            for (Element ignore_js_op : ignore_js_ops) {
                Elements imgs = ignore_js_op.getElementsByTag("img");
                for (Element img : imgs) {
                    String src = img.getElementsByTag("img").attr("file");
                    if (!"".equals(src)){
                        imgsArray.add(src);
                    }
                }
            }
        }
        return imgsArray;
    }

    public static HashMap<String, String> getImgHashMap(String url) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        Document document = null;
        System.out.println("---开始解析网页---");

        int i = 1;
        try {
            document = Jsoup.parse(new URL(url), 40000);

        } catch (IOException e) {
            e.printStackTrace();
        }

        hashMap.put("fileFolderName", "\\反差婊");


        Element elementById = document.getElementById("idstpc");
        Elements imgS = elementById.getElementsByTag("img");
        for (Element img : imgS) {
            String src = img.attr("src");
            if (!"".equals(src)) {
                hashMap.put("imgUrl" + (i++), src);
            }
        }
        System.out.println("--网页解析完成，共搜索到" + (hashMap.size() - 1) + "张你想要的图片--");
        return hashMap;
    }
/*
        Elements elementsByClass = document.getElementsByClass("post-bd");
        for (Element byClass : elementsByClass) {
            Elements ignore_js_ops = byClass.getElementsByTag("ignore_js_op");
            for (Element ignore_js_op : ignore_js_ops) {
                Elements imgs = ignore_js_op.getElementsByTag("img");
                for (Element img : imgs) {
                    String src = img.getElementsByTag("img").attr("file");
                    if (!"".equals(src)){
                        hashMap.put("imgUrl"+(i++),src);
                    }
                }
            }
        }
        System.out.println("--网页解析完成，共搜索到"+(hashMap.size()-1)+"张你想要的图片--");
        return hashMap;
    }
*/
    public static void main(String[] args) {
        String url = new Scanner(System.in).next();
        HashMap<String, String> imgArray2 = JsoupSpider2.getImgHashMap(url);
        //System.out.println(imgArray2);
        for (Map.Entry<String, String> entry : imgArray2.entrySet()) {
            System.out.println(entry.getKey()+" :  "+entry.getValue());
        }

    }
}
